package com.dsbridge.enrollment.dao;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.dsbridge.enrollment.model.CoursesDTO;
import com.dsbridge.enrollment.model.StudentsDTO;

@Repository("courseDAO")
public class CourseDAO {

	// SimpleJDBCTemplate and SimpleJDBCInsert
	private SimpleJdbcTemplate simpleJdbcTemplate;

	private SimpleJdbcInsert courseInsert;

	@Autowired
	private DataSource dataSource;

	// Insert
	// Update
	// Read
	public int create(CoursesDTO course) {
		// Need to return the Student ID - Once it created the Student.
		/*
		 * simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); // Insert
		 * SQL Query String insertSQL =
		 * "INSERT INTO students (date_of_registartion,date_of_latest_login,login_name,password,first_name,middle_name,last_name)	VALUES (?,?,?,?,?,?,?)"
		 * ; int i = simpleJdbcTemplate.update(insertSQL,
		 * student.getRegistrationDate(), student.getLastLogin(),
		 * student.getLoginName(), student.getPassword(),
		 * student.getFirstName(), student.getMiddleName(),
		 * student.getLastName());
		 */
		courseInsert = new SimpleJdbcInsert(dataSource)
				.withTableName("courses")
				.usingColumns("course_name", 
						"course_description", "subject_id", "author_id").usingGeneratedKeyColumns("course_id");
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("course_name", course.getCourseName());
		map.put("course_description", course.getCourseDesc());
		map.put("subject_id", course.getSubjectID());
		map.put("author_id", course.getAuthorID());
		
		
		Number number=courseInsert.executeAndReturnKey(map);
		

		return number.intValue();
	}

}
